<?php
class UsersController extends AppController {
    var $helpers = array ('Html','Form');
    var $name = 'Users'; // riferimento alla cartella delle view
    
    

    public function beforeFilter(){
    	
		parent::beforeFilter();
		
/*		if ($this->Session->read("user")==null) {
			$this->Session->setFlash("Sessione scaduta!");
			$this->redirect("/Login");
		}*/ 

    }

	// public function beforeRender(){
		// if(isset($this->viewVars['users'])){
			// foreach ($this->viewVars['users'] as $chiave=>$user) {
				// $ret = Multa::getDescrizioneGruppo($user['User']['GRUPPO_ID']);
				// $this->viewVars['users'][$chiave]['User']['GRUPPO']=$ret;
			// } 
		// }
	// }

    
    public function index() {
    	//la classe User è stata già istanziata dal controller perche abbiamo seguito le regole di denominazione di cake
        $this->set('users', $this->User->find('all', array('recursive' => 0)));
    }
	
/*    public function view($id='0') {

    	if ($id == null || !isset($id)) {
			$this->redirect("/users/view");
    	} else {
			$this->User->id = $id;
			$this->set('user', $this->User->read());
		}
    }*/

    public function add() {
    	
    	Controller::loadModel('Group');
		$groups = $this->Group->find('list', array('fields' => array('Group.ID', 'Group.descrizione'))	);
		$this->set('groups',$groups);

		// print_r($groups);die;
		
//    	Controller::loadModel('Users');
		
		
    }

    public function edit($id) {

    	Controller::loadModel('Group');
		$groups = $this->Group->find('list', array('fields' => array('Group.ID', 'Group.descrizione'))	);
		$this->set('groups',$groups);
    	
		$this->User->id = $id;
		$this->set('user', $this->User->read());

    }

    public function save() {

		//$this->User->read(null,$this->params['ID']);
		//$this->User->set('LOGIN', $this->params['LOGIN']);
		if (isset($this->request->data['id'])) {
			$this->User->read(null,$this->request->data['id']);
		} else {
			$this->User->set('username', $this->request->data['username']);
			$pwd = $this->generate_id();
			$this->User->set('registrazione', $pwd);
			if (isset($this->request->data['password'])) {
				$this->User->set('password', $this->Auth->password($this->request->data['password']));
			}
		}
		$this->User->set('group_id', $this->request->data['group_id']);
		$this->User->save();
		$this->Session->setFlash('User Saved!');
		$this->redirect("/users");
    }

    public function delete($id='') {
		if ($id=='')
			$id = $this->request->data['id'];
		$this->User->delete($id);
		$this->Session->setFlash('User Deleted!');
		$this->redirect("/users");

    }

	public function generate_id() { 
	 $salt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; 
	  srand((double)microtime()*1000000); 
	 $id = ""; 
	//generates random 6 character id  
	  for($count = 0; $count < 6; $count++) 
	 { 
	    $num = rand() % 33; 
	    $tmp = substr($salt, $num, 1); 
	    $id = $id.$tmp; 
	  } 
	  return $id; 
	}  

	function login() {
	    //Auth Magic
	    if ($this->request->is('post')) {
	    // print_r($this->request);die;	
			
	        if ($this->Auth->login()) {
	            return $this->redirect($this->Auth->redirect());
	            // Prior to 2.3 use `return $this->redirect($this->Auth->redirect());`
	        } else {
	            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
	        }
	    }
	}

	function logout() {
	    $this->redirect($this->Auth->logout());
	}
}
?>